package com.lhf.novel.sysuser.controller;

import com.github.pagehelper.PageInfo;
import com.lhf.novel.sysuser.entity.SysRole;
import com.lhf.novel.sysuser.service.SysRoleService;
import com.lhf.novel.sysuser.vo.SysRoleInfoVo;
import com.lhf.novel.sysuser.vo.SysRoleListVo;
import com.lhf.novel.util.Result;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (SysRole)表控制层
 *
 * @author 刘会发
 * @since 2020-03-09 11:20:00
 */
@RestController
@RequestMapping("sysRole")
@Api(tags = "角色操作控制层")
public class SysRoleController {
    /**
     * 服务对象
     */
    @Resource
    private SysRoleService sysRoleService;


    @PostMapping("add")
    @ApiOperation("添加角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名字", dataTypeClass = String.class, example = "管理员"),
            @ApiImplicitParam(name = "role", value = "角色", dataTypeClass = String.class, example = "ADMIN"),
            @ApiImplicitParam(name = "roleIco", value = "角色图标", dataTypeClass = String.class, example = "el-icon-eleme"),
            @ApiImplicitParam(name = "jsonPids", value = "权限集合json", dataTypeClass = String.class, example = "[1,2,3,4,5]"),

    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限")
    })
    public Result<SysRole> insert(String roleName,
                                  String role,
                                  @RequestParam(required = false) String roleIco,
                                  String jsonPids) {
        return Result.success(sysRoleService.insert(new SysRole(roleName, role, roleIco), jsonPids));
    }


    @PutMapping("update")
    @ApiOperation("修改角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rid", value = "角色id", dataTypeClass = Integer.class, example = "1", required = true),
            @ApiImplicitParam(name = "roleName", value = "角色名字", dataTypeClass = String.class, example = "管理员"),
            @ApiImplicitParam(name = "role", value = "角色", dataTypeClass = String.class, example = "ADMIN"),
            @ApiImplicitParam(name = "roleIco", value = "角色图标", dataTypeClass = String.class, example = "el-icon-eleme"),
            @ApiImplicitParam(name = "jsonPids", value = "权限集合json", dataTypeClass = String.class, example = "[1,2,3,4,5]"),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限")
    })
    public Result<Integer> update(Integer rid,
                                  @RequestParam(required = false) String roleName,
                                  @RequestParam(required = false) String role,
                                  @RequestParam(required = false) String roleIco,
                                  @RequestParam(required = false) String jsonPids) throws IllegalAccessException {
        return Result.success(sysRoleService.update(new SysRole(rid, roleName, role, roleIco), jsonPids));
    }

    @PatchMapping("lock")
    @ApiOperation("锁定角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rid", value = "角色id", dataTypeClass = Integer.class, example = "1"),
            @ApiImplicitParam(name = "isDel", value = "是否删除", dataTypeClass = Integer.class, example = "1"),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限")
    })
    public Result<Integer> lock(Integer rid, Integer isDel) throws IllegalAccessException {
        return Result.success(sysRoleService.update(new SysRole(rid, isDel), null));
    }

    @GetMapping("queryAll")
    @ApiOperation("条件查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名字", dataTypeClass = String.class, example = "管理员"),
            @ApiImplicitParam(name = "role", value = "角色", dataTypeClass = String.class, example = "ADMIN"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataTypeClass = Integer.class, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页展示数量", dataTypeClass = Integer.class, example = "10"),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限")
    })
    public Result<PageInfo<SysRoleListVo>> queryAll(@RequestParam(required = false) String roleName,
                                                    @RequestParam(required = false) String role,
                                                    @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                    @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(sysRoleService.queryAll(roleName, role, pageNum, pageSize));
    }

    @GetMapping("queryById")
    @ApiOperation("查询角色详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色id", dataTypeClass = Integer.class, example = "1", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限")
    })
    public Result<SysRoleInfoVo> queryById(Integer id) {
        return Result.success(sysRoleService.queryById(id));
    }

    @PostMapping("authorization")
    @ApiOperation("角色授权")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rid", value = "角色id", dataTypeClass = Integer.class, example = "1"),
            @ApiImplicitParam(name = "pidJson", value = "权限id数组", dataTypeClass = String.class, example = "[1,2,3]")
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限")
    })
    public Result<Integer> authorization(Integer rid, String pidJson) {
        return Result.success(sysRoleService.authorization(rid, pidJson));
    }

    @DeleteMapping("delete")
    @ApiOperation("删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rid", value = "角色id", dataTypeClass = Integer.class, example = "1",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "服务端异常"),
            @ApiResponse(code = 4005, message = "无权限")
    })
    public Result<Boolean> delete(Integer rid) {
        return Result.success(sysRoleService.deleteById(rid));
    }

}